class Solution {
    public boolean isSymmetric(TreeNode root) {
        return mirror(root, root);
    }

    public boolean mirror(TreeNode a, TreeNode b) {
        if (a == null && b == null) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }

        return (a.val == b.val)
                && mirror(a.left, b.right)
                && mirror(a.right, b.left);
    }
}

// 定义二叉树节点类
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

public class SymmetricBinaryTree {
    public static void main(String[] args) {
        // 构造对称二叉树 [1,2,2,3,4,4,3]
        TreeNode symmetricRoot = buildSymmetricTree();
        Solution solution = new Solution();
        System.out.println("对称二叉树是否对称: " + solution.isSymmetric(symmetricRoot));

        // 构造非对称二叉树 [1,2,2,null,3,null,3]
        TreeNode asymmetricRoot = buildAsymmetricTree();
        System.out.println("非对称二叉树是否对称: " + solution.isSymmetric(asymmetricRoot));
    }

    // 构造对称二叉树
    private static TreeNode buildSymmetricTree() {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);
        return root;
    }

    // 构造非对称二叉树
    private static TreeNode buildAsymmetricTree() {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.right = new TreeNode(3);
        root.right.right = new TreeNode(3);
        return root;
    }
}